using AutoMapper;
using Medical.Api.Write.Application.Command.BasicManagement.DeviceClassificationDictionaryCommand;
using Medical.Api.Write.Application.Command.BasicManagement.DeviceDictionaryCommand;
using Medical.Api.Write.Application.Command.BasicManagement.EquipmentPackageDictionaryCommand;
using Medical.Api.Write.Application.Command.BasicManagement.ShelfDictionaryCommand;
using Medical.Api.Write.Dto.BasicManagement.DeviceClassificationDictionaryDto;
using Medical.Api.Write.Dto.BasicManagement.DeviceDictionaryDto;
using Medical.Api.Write.Dto.BasicManagement.EquipmentPackageDictionaryDto;
using Medical.Api.Write.Dto.BasicManagement.ShelfDictionaryDto;
using Medical.Domain.BasicManagement;

namespace Medical.Api.Write.AutoMapper
{
    /// <summary>
    /// 基础管理模块的AutoMapper配置类
    /// 配置DTO与领域实体之间的映射关系
    /// </summary>
    public class BasicManagementMapper : Profile
    {
        /// <summary>
        /// 构造函数，在此配置所有映射关系
        /// </summary>
        public BasicManagementMapper()
        {
            // 设备相关映射：CreateEquipmentDto -> Equipment
            CreateMap<CreateEquipmentDto, Equipment>()
                .ForMember(dest => dest.Id, opt => opt.Ignore()) // Id由数据库自动生成
                .ForMember(dest => dest.CreateTime, opt => opt.Ignore()) // 创建时间由系统设置
                .ForMember(dest => dest.UpdateTime, opt => opt.Ignore()) // 更新时间由系统设置
                .ForMember(dest => dest.IsDeleted, opt => opt.Ignore()); // 删除标记默认false

            // 设备状态更新映射：UpdateEquipmentStatusDto -> Equipment
            CreateMap<UpdateEquipmentStatusDto, Equipment>()
                .ForMember(dest => dest.EquipmentCode, opt => opt.Ignore())
                .ForMember(dest => dest.EquipmentName, opt => opt.Ignore())
                .ForMember(dest => dest.EquipmentType, opt => opt.Ignore())
                .ForMember(dest => dest.Manufacturer, opt => opt.Ignore())
                .ForMember(dest => dest.CreateTime, opt => opt.Ignore())
                .ForMember(dest => dest.CreateUserId, opt => opt.Ignore())
                .ForMember(dest => dest.CreateUserName, opt => opt.Ignore())
                .ForMember(dest => dest.UpdateTime, opt => opt.MapFrom(_ => DateTime.Now))
                .ForMember(dest => dest.UpdateUserId, opt => opt.Ignore())  // 保留原有UpdateUserId
                .ForMember(dest => dest.UpdateUserName, opt => opt.Ignore())  // 保留原有UpdateUserName
                .ForMember(dest => dest.IsDeleted, opt => opt.Ignore());

            // 设备删除映射：DeleteEquipmentCommand -> Equipment
            CreateMap<DeleteEquipmentCommand, Equipment>()
                .ForMember(dest => dest.EquipmentCode, opt => opt.Ignore())
                .ForMember(dest => dest.EquipmentName, opt => opt.Ignore())
                .ForMember(dest => dest.EquipmentType, opt => opt.Ignore())
                .ForMember(dest => dest.Manufacturer, opt => opt.Ignore())
                .ForMember(dest => dest.Status, opt => opt.Ignore())
                .ForMember(dest => dest.CreateTime, opt => opt.Ignore())
                .ForMember(dest => dest.CreateUserId, opt => opt.Ignore())
                .ForMember(dest => dest.CreateUserName, opt => opt.Ignore())
                .ForMember(dest => dest.UpdateTime, opt => opt.MapFrom(_ => DateTime.Now))
                .ForMember(dest => dest.UpdateUserId, opt => opt.Ignore())
                .ForMember(dest => dest.UpdateUserName, opt => opt.Ignore())
                .ForMember(dest => dest.IsDeleted, opt => opt.MapFrom(_ => true));

            // 设备信息更新映射：UpdateEquipmentCommand -> Equipment
            CreateMap<UpdateEquipmentCommand, Equipment>()
                .ForMember(dest => dest.Status, opt => opt.Ignore())
                .ForMember(dest => dest.CreateTime, opt => opt.Ignore())
                .ForMember(dest => dest.CreateUserId, opt => opt.Ignore())
                .ForMember(dest => dest.CreateUserName, opt => opt.Ignore())
                .ForMember(dest => dest.UpdateTime, opt => opt.MapFrom(_ => DateTime.Now))
                .ForMember(dest => dest.IsDeleted, opt => opt.Ignore());

            // 设备信息更新映射：UpdateEquipmentDto -> Equipment
            CreateMap<UpdateEquipmentDto, Equipment>()
                .ForMember(dest => dest.Status, opt => opt.Ignore())
                .ForMember(dest => dest.CreateTime, opt => opt.Ignore())
                .ForMember(dest => dest.CreateUserId, opt => opt.Ignore())
                .ForMember(dest => dest.CreateUserName, opt => opt.Ignore())
                .ForMember(dest => dest.UpdateTime, opt => opt.MapFrom(_ => DateTime.Now))
                .ForMember(dest => dest.IsDeleted, opt => opt.Ignore());

            // 货架创建映射：CreateShelvesDto -> Shelves
            CreateMap<CreateShelvesDto, Shelves>()
                .ForMember(dest => dest.CreateTime, opt => opt.Ignore())
                .ForMember(dest => dest.CreateUserId, opt => opt.Ignore())
                .ForMember(dest => dest.CreateUserName, opt => opt.Ignore())
                .ForMember(dest => dest.UpdateTime, opt => opt.Ignore())
                .ForMember(dest => dest.IsDeleted, opt => opt.Ignore());

            // 货架状态更新映射：UpdateShelvesStatusDto -> Shelves
            CreateMap<UpdateShelvesStatusDto, Shelves>()
                .ForMember(dest => dest.ShelfCode, opt => opt.Ignore())
                .ForMember(dest => dest.ShelfName, opt => opt.Ignore())
                .ForMember(dest => dest.ParentShelfId, opt => opt.Ignore())
                .ForMember(dest => dest.CreateTime, opt => opt.Ignore())
                .ForMember(dest => dest.CreateUserId, opt => opt.Ignore())
                .ForMember(dest => dest.CreateUserName, opt => opt.Ignore())
                .ForMember(dest => dest.UpdateTime, opt => opt.MapFrom(_ => DateTime.Now))
                .ForMember(dest => dest.IsDeleted, opt => opt.Ignore());

            // 货架信息更新映射：UpdateShelvesDto -> Shelves
            CreateMap<UpdateShelvesDto, Shelves>()
                .ForMember(dest => dest.Status, opt => opt.Ignore())
                .ForMember(dest => dest.CreateTime, opt => opt.Ignore())
                .ForMember(dest => dest.CreateUserId, opt => opt.Ignore())
                .ForMember(dest => dest.CreateUserName, opt => opt.Ignore())
                .ForMember(dest => dest.UpdateTime, opt => opt.MapFrom(_ => DateTime.Now))
                .ForMember(dest => dest.IsDeleted, opt => opt.Ignore());

            // 货架删除映射：DeleteShelvesCommand -> Shelves
            CreateMap<DeleteShelvesCommand, Shelves>()
                .ForMember(dest => dest.ShelfCode, opt => opt.Ignore())
                .ForMember(dest => dest.ShelfName, opt => opt.Ignore())
                .ForMember(dest => dest.ParentShelfId, opt => opt.Ignore())
                .ForMember(dest => dest.Status, opt => opt.Ignore())
                .ForMember(dest => dest.CreateTime, opt => opt.Ignore())
                .ForMember(dest => dest.CreateUserId, opt => opt.Ignore())
                .ForMember(dest => dest.CreateUserName, opt => opt.Ignore())
                .ForMember(dest => dest.UpdateTime, opt => opt.MapFrom(_ => DateTime.Now))
                .ForMember(dest => dest.IsDeleted, opt => opt.MapFrom(_ => true));

            // 器械分类相关映射：CreateApparatusTypeDto -> ApparatusType
            CreateMap<CreateApparatusTypeDto, ApparatusType>()
                .ForMember(dest => dest.Id, opt => opt.Ignore()) // Id由数据库自动生成
                .ForMember(dest => dest.CreateTime, opt => opt.Ignore()) // 创建时间由系统设置
                .ForMember(dest => dest.UpdateTime, opt => opt.Ignore()) // 更新时间由系统设置
                .ForMember(dest => dest.IsDeleted, opt => opt.Ignore()) // 删除标记默认false
                .ForMember(dest => dest.CreateTime, opt => opt.Ignore())
                .ForMember(dest => dest.CreateUserId, opt => opt.Ignore())
                .ForMember(dest => dest.CreateUserName, opt => opt.Ignore());

            // 器械分类更新映射：UpdateApparatusTypeDto -> ApparatusType
            CreateMap<UpdateApparatusTypeDto, ApparatusType>()
                .ForMember(dest => dest.CreateTime, opt => opt.Ignore())
                .ForMember(dest => dest.CreateUserId, opt => opt.Ignore())
                .ForMember(dest => dest.CreateUserName, opt => opt.Ignore())
                .ForMember(dest => dest.UpdateTime, opt => opt.MapFrom(_ => DateTime.Now))
                .ForMember(dest => dest.IsDeleted, opt => opt.Ignore());

            // 器械分类删除映射：DeleteApparatusTypeCommand -> ApparatusType
            CreateMap<DeleteApparatusTypeCommand, ApparatusType>()
                .ForMember(dest => dest.Name, opt => opt.Ignore())
                .ForMember(dest => dest.Code, opt => opt.Ignore())
                .ForMember(dest => dest.ParentId, opt => opt.Ignore())
                .ForMember(dest => dest.Status, opt => opt.Ignore())
                .ForMember(dest => dest.CreateTime, opt => opt.Ignore())
                .ForMember(dest => dest.CreateUserId, opt => opt.Ignore())
                .ForMember(dest => dest.CreateUserName, opt => opt.Ignore())
                .ForMember(dest => dest.UpdateTime, opt => opt.MapFrom(_ => DateTime.Now))
                .ForMember(dest => dest.IsDeleted, opt => opt.MapFrom(_ => true));

            // 器械相关映射：CreateApparatusDto -> Apparatus
            CreateMap<CreateApparatusDto, Apparatus>()
                .ForMember(dest => dest.Id, opt => opt.Ignore()) // Id由数据库自动生成
                .ForMember(dest => dest.Status, opt => opt.Ignore()) // 状态默认为启用
                .ForMember(dest => dest.CreateTime, opt => opt.Ignore()) // 创建时间由系统设置
                .ForMember(dest => dest.UpdateTime, opt => opt.Ignore()) // 更新时间由系统设置
                .ForMember(dest => dest.IsDeleted, opt => opt.Ignore()) // 删除标记默认false
                .ForMember(dest => dest.CreateUserId, opt => opt.Ignore())
                .ForMember(dest => dest.CreateUserName, opt => opt.Ignore())
                .ForMember(dest => dest.UpdateUserId, opt => opt.Ignore())
                .ForMember(dest => dest.UpdateUserName, opt => opt.Ignore());

            // 器械更新映射：UpdateApparatusDto -> Apparatus
            CreateMap<UpdateApparatusDto, Apparatus>()
                .ForMember(dest => dest.Status, opt => opt.Ignore()) // 保持状态不变
                .ForMember(dest => dest.CreateTime, opt => opt.Ignore())
                .ForMember(dest => dest.CreateUserId, opt => opt.Ignore())
                .ForMember(dest => dest.CreateUserName, opt => opt.Ignore())
                .ForMember(dest => dest.UpdateTime, opt => opt.MapFrom(_ => DateTime.Now))
                .ForMember(dest => dest.UpdateUserId, opt => opt.Ignore())
                .ForMember(dest => dest.UpdateUserName, opt => opt.Ignore())
                .ForMember(dest => dest.IsDeleted, opt => opt.Ignore()); // 保持删除标识不变

            // 器械包相关映射：CreateEquipmentPackageDto -> EquipmentPackage
            CreateMap<CreateEquipmentPackageDto, EquipmentPackage>()
                .ForMember(dest => dest.Id, opt => opt.Ignore()) // Id由数据库自动生成
                .ForMember(dest => dest.PackageCode, opt => opt.Ignore()) // 器械包编码由后端自动生成
                .ForMember(dest => dest.Barcodes, opt => opt.Ignore()) // 条码由系统生成
                .ForMember(dest => dest.Status, opt => opt.MapFrom(_ => 1)) // 状态默认为启用
                .ForMember(dest => dest.CreateTime, opt => opt.MapFrom(_ => DateTime.Now))
                .ForMember(dest => dest.UpdateTime, opt => opt.MapFrom(_ => DateTime.Now))
                .ForMember(dest => dest.IsDeleted, opt => opt.MapFrom(_ => false))
                .ForMember(dest => dest.CreateUserId, opt => opt.Ignore())
                .ForMember(dest => dest.CreateUserName, opt => opt.Ignore())
                .ForMember(dest => dest.UpdateUserId, opt => opt.Ignore())
                .ForMember(dest => dest.UpdateUserName, opt => opt.Ignore());

            // 器械包更新映射：UpdateEquipmentPackageDto -> EquipmentPackage
            CreateMap<UpdateEquipmentPackageDto, EquipmentPackage>()
                .ForMember(dest => dest.PackageCode, opt => opt.Ignore()) // 保持编码不变
                .ForMember(dest => dest.Barcodes, opt => opt.Ignore()) // 保持条码不变
                .ForMember(dest => dest.Status, opt => opt.Ignore()) // 保持状态不变
                .ForMember(dest => dest.CreateTime, opt => opt.Ignore())
                .ForMember(dest => dest.CreateUserId, opt => opt.Ignore())
                .ForMember(dest => dest.CreateUserName, opt => opt.Ignore())
                .ForMember(dest => dest.UpdateTime, opt => opt.MapFrom(_ => DateTime.Now))
                .ForMember(dest => dest.UpdateUserId, opt => opt.Ignore())
                .ForMember(dest => dest.UpdateUserName, opt => opt.Ignore())
                .ForMember(dest => dest.IsDeleted, opt => opt.Ignore()); // 保持删除标识不变
        }
    }
}
